package com.xinbochuang.machine.admin.service.impl.macRoom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.macRoom.ContrastEquipmentDetail;
import com.xinbochuang.machine.admin.domain.macRoom.EquCabinet;
import com.xinbochuang.machine.admin.domain.macRoom.Equipment;
import com.xinbochuang.machine.admin.domain.macRoom.EquipmentDetail;
import com.xinbochuang.machine.admin.dto.ContrastEquDto;
import com.xinbochuang.machine.admin.dto.EquipmentDetailDto;
import com.xinbochuang.machine.admin.mapper.macRoom.ContrastEquipmentDetailMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.EquipmentDetailMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.EquipmentMapper;
import com.xinbochuang.machine.admin.service.macRoom.IContrastEquipmentDetailService;
import com.xinbochuang.machine.common.utils.StringUtils;

/**
 *
 * @author 杜文博
 * @date 2020-09-21
 */
@Service
public class ContrastEquipmentDetailServiceImpl extends ServiceImpl<ContrastEquipmentDetailMapper, ContrastEquipmentDetail> implements IContrastEquipmentDetailService {

	@Autowired
	private ContrastEquipmentDetailMapper contrastEquipmentDetailMapper;

	@Autowired
	EquipmentDetailMapper equipmentDetailMapper;

	@Autowired
	EquipmentMapper equipmentMapper;

	@Override
	public List<ContrastEquDto> selectContrasEquList(ContrastEquDto dto) {
		String cabinetId = dto.getCabinetId();
		String planId = dto.getPlanId();
		String pageNum = dto.getPageNum().toString();
		String pageSize = dto.getPageSize().toString();
		List<ContrastEquDto> list = contrastEquipmentDetailMapper.selectContrastEquList(cabinetId,planId,pageNum, pageSize);
		return list;
	}

	@Override
	public Integer selectContrasEquCount(ContrastEquDto dto) {
		String cabinetId = dto.getCabinetId();
		String planId = dto.getPlanId();
		String pageNum = dto.getPageNum().toString();
		String pageSize = dto.getPageSize().toString();
		Integer counts = contrastEquipmentDetailMapper.selectContrastEquCounts(cabinetId,planId,pageNum,
			pageSize);
		return counts;
	}

	@Override
	public Integer updatEquDetail(Integer checkKey, String equIds,String planId) {
		//checkKey为0不修改原始数据，1为修改数据
		String [] equIdList = equIds.split(",",-1);
		ContrastEquipmentDetail contrastEquipmentDetail = new ContrastEquipmentDetail();
		Integer result = null;
		String cabinetId = null;
		EquCabinet equCabinet = new EquCabinet();
		List<EquipmentDetailDto> equDetailDtoList = new ArrayList<>();
		EquipmentDetail equipmentDetail = new EquipmentDetail();
		Equipment equipment = new Equipment();
//		if(checkKey!=0&& StringUtils.isNotEmpty(equCabinetId)){
//			List<String> oldEquIdsList = new ArrayList<>();
//			//根据macId获取原表cabientId
//			oldEquIdsList =  equipmentMapper.selectEquIdsByCabientId(equCabinetId);
//			//对比设备表UUID与核验表euqId取交集之外的集合
//			Collection<String> col = CollectionUtils.union(Arrays.asList(equIdList),oldEquIdsList);
//			col =CollectionUtils.disjunction(Arrays.asList(equIdList),oldEquIdsList);
//			for(String str:col){
//				Equipment surplusEquipment = new Equipment();
//				EquipmentDetail surplusEquDetail = new EquipmentDetail();
//				QueryWrapper<EquipmentDetail> warrperEquDetail = new QueryWrapper<>();
//				surplusEquipment = equipmentMapper.selectById(str);
//				surplusEquipment.setDeleteFlag(1);
//				//删除设备表原始数据
//				equipmentMapper.updateById(surplusEquipment);
//				warrperEquDetail.lambda().eq(EquipmentDetail::getEquId,str);
//				surplusEquDetail = equipmentDetailMapper.selectOne(warrperEquDetail);
//				surplusEquDetail.setDeleteFlag(1);
//				//删除资产表原始数据
//				equipmentDetailMapper.updateById(surplusEquDetail);
//			}
//		}
	//根据设备Id查询机柜核查表内optType 若为2则为删除操作，则删除原始数据
		if(checkKey!=0){
			for(int i = 0; i < equIdList.length; i++){
				Equipment surplusEquipment = new Equipment();
				EquipmentDetail surplusEquDetail = new EquipmentDetail();
				ContrastEquipmentDetail contrastEquipmentDetail1 = new ContrastEquipmentDetail();
				QueryWrapper<ContrastEquipmentDetail> warrperEquDaetail = new QueryWrapper<>();
				warrperEquDaetail.lambda().eq(ContrastEquipmentDetail::getEquId,equIdList[i]);
				contrastEquipmentDetail1 = contrastEquipmentDetailMapper.selectOne(warrperEquDaetail);
				String optType = contrastEquipmentDetail1.getOptType();
				if(optType.equals("2")){
					QueryWrapper<EquipmentDetail> warrperEquDetail = new QueryWrapper<>();
					surplusEquipment = equipmentMapper.selectById(equIdList[i]);
					surplusEquipment.setDeleteFlag(1);
					//删除设备表原始数据
					equipmentMapper.updateById(surplusEquipment);
					warrperEquDetail.lambda().eq(EquipmentDetail::getEquId,equIdList[i]);
					surplusEquDetail = equipmentDetailMapper.selectOne(warrperEquDetail);
					surplusEquDetail.setDeleteFlag(1);
					//删除资产表原始数据
					equipmentDetailMapper.updateById(surplusEquDetail);
				}

			}
		}
		if(checkKey==0) {
			for (int i = 0; i < equIdList.length; i++) {
				//根据设备Id 查询核查表内设备详情
				contrastEquipmentDetail = contrastEquipmentDetailMapper.selectByPlanId(equIdList[i],planId);
				if (contrastEquipmentDetail != null) {
					contrastEquipmentDetail.setCheckState(1);
					result = contrastEquipmentDetailMapper.updateById(contrastEquipmentDetail);
				}
			}
		}else if (checkKey==1){
			for (int i = 0; i < equIdList.length; i++) {
				//根据设备Id,查询核查表设备详情
				contrastEquipmentDetail = contrastEquipmentDetailMapper.selectByPlanId(equIdList[i],planId);
				if (contrastEquipmentDetail != null) {
					contrastEquipmentDetail.setCheckState(1);
					result = contrastEquipmentDetailMapper.updateById(contrastEquipmentDetail);
					equipment = equipmentMapper.selectById(contrastEquipmentDetail.getEquId());
					equipmentDetail = equipmentDetailMapper.selectByEquId(contrastEquipmentDetail.getEquId());
					if(equipment!=null){
						equipment.setEquState(contrastEquipmentDetail.getEquState());
						equipment.setEquName(contrastEquipmentDetail.getEquName());
						equipment.setEquHeight(contrastEquipmentDetail.getEquHeight());
						equipment.setCabinetId(contrastEquipmentDetail.getCabinetId());
						equipment.setUpdateTime(DateUtil.now());
						equipmentMapper.updateById(equipment);
					}
					if(equipmentDetail!=null){
						equipmentDetail.setCabinetName(contrastEquipmentDetail.getCabinetName());
						equipmentDetail.setAssetsBigType(contrastEquipmentDetail.getAssetsBigType());
						equipmentDetail.setAssetsSmallType(contrastEquipmentDetail.getAssetsSmallType());
						equipmentDetail.setAssetsNumber(contrastEquipmentDetail.getAssetsNumber());
						equipmentDetail.setUfloorNum(contrastEquipmentDetail.getUfloorNum());
						equipmentDetail.setCabinetId(contrastEquipmentDetail.getCabinetId());
						equipmentDetail.setEquId(contrastEquipmentDetail.getEquId());
						equipmentDetail.setAssetsState(contrastEquipmentDetail.getAssetsState());
						equipmentDetail.setDepartment(contrastEquipmentDetail.getDepartment());
						equipmentDetail.setMacRoomName(contrastEquipmentDetail.getMacRoomName());
						equipmentDetail.setModelType(contrastEquipmentDetail.getModelType());
						equipmentDetail.setOffice(contrastEquipmentDetail.getOffice());
						equipmentDetail.setOpenDate(contrastEquipmentDetail.getOpenDate());
						equipmentDetail.setOwnerSystem(contrastEquipmentDetail.getOwnerSystem());
						equipmentDetail.setPrincipal(contrastEquipmentDetail.getPrincipal());
						equipmentDetail.setUpdateTime(DateUtil.now());
						equipmentDetailMapper.updateById(equipmentDetail);
					}
				}
			}
		}
		return result;
	}
}
